package com.ruoyi.project.warehouse.service.impl;

import java.util.Date;
import java.util.List;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.project.warehouse.domain.*;
import com.ruoyi.project.warehouse.mapper.RyDepotRecordAuditMapper;
import com.ruoyi.project.warehouse.mapper.RyInventoryCurrentStockMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.project.warehouse.mapper.OutRyDepotItemMapper;
import com.ruoyi.project.warehouse.service.IOutRyDepotItemService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 出库Service业务层处理
 *
 * @author you
 * @date 2022-09-07
 */
@Service
public class OutRyDepotItemServiceImpl implements IOutRyDepotItemService
{
    @Autowired
    private OutRyDepotItemMapper outRyDepotItemMapper;

    @Autowired
    private RyInventoryCurrentStockMapper ryInventoryCurrentStockMapper;

    @Autowired
    private RyDepotRecordAuditMapper ryDepotRecordAuditMapper;

    /**
     * 查询出库
     *
     * @param id 出库主键
     * @return 出库
     */
    @Override
    public OutRyDepotItem selectOutRyDepotItemById(Long id)
    {
        return outRyDepotItemMapper.selectOutRyDepotItemById(id);
    }

    /**
     * 查询出库列表
     *
     * @param outRyDepotItem 出库
     * @return 出库
     */
    @Override
    public List<RyDepotItemVo> selectOutRyDepotItemList(OutRyDepotItem outRyDepotItem)
    {
        RyDepotRecordAudit ryDepotRecordAudit = new RyDepotRecordAudit();

        List<RyDepotItemVo> ryDepotItemVos = outRyDepotItemMapper.selectOutRyDepotItemList(outRyDepotItem);

        for (RyDepotItemVo ryDepotItemVo : ryDepotItemVos) {
            //设置初始status为0
            ryDepotItemVo.setStatus("0");
            //如果该出库记录是项目关联的，设置status为1
            if(ryDepotItemVo.getHeaderId()!=null){
                ryDepotItemVo.setStatus("1");
            }

            if(ryDepotItemVo.getTenantId()!=null){
                RyDepotItemVo ryDepotItemVo1 = outRyDepotItemMapper.selectOutRyDepotItemByTenantId(ryDepotItemVo.getId());
                ryDepotItemVo.setProjectCode(ryDepotItemVo1.getProjectCode());
                ryDepotItemVo.setProjectName(ryDepotItemVo1.getProjectName());
                //ryDepotItemVo.setStatus("1");//如果该出库记录是项目关联的，设置status为1
            }

            ryDepotRecordAudit.setDepotitemId(ryDepotItemVo.getId());
            ryDepotRecordAudit.setAuditStatus(0);
            //如果该入库记录正在审核中，设置status为1
            List<RyDepotRecordAuditVo> ryDepotRecordAuditVos = ryDepotRecordAuditMapper.selectRyDepotRecordAuditList(ryDepotRecordAudit);
            if(ryDepotRecordAuditVos.size()>0){
                ryDepotItemVo.setStatus("1");
            }
        }
        return ryDepotItemVos;
    }

    /**
     * 新增出库
     *
     * @param ryDepotItemVo 出库
     * @return 结果
     */
    @Override
    @Transactional
    public int insertOutRyDepotItem(RyDepotItemVo ryDepotItemVo)
    {
        //获取仓库id和商品id
        Long depotId = ryDepotItemVo.getDepotId();
        Long inventoryId = ryDepotItemVo.getInventoryId();
        RyInventoryCurrentStock paramstock = new RyInventoryCurrentStock();
        paramstock.setDepotId(depotId);
        paramstock.setInventoryId(inventoryId);
        //根据depotId和InventoryId查询出当前库存信息
        RyInventoryCurrentStock result = ryInventoryCurrentStockMapper.getCurrentStockByDepotInventId(paramstock);
        System.out.println("--> result:"+result);

        //创建参数对象
        RyInventoryCurrentStock param = new RyInventoryCurrentStock();

        //计算需要更改的总库数量
        //获取原库存数
        Double oldNum = result.getCurrentNumber();
        //获取新出库数量
        Double outNum = ryDepotItemVo.getOperNumber();
        //需要更改的数量
        Double updateNum = oldNum - outNum;
        param.setCurrentNumber(updateNum);

        //执行修改语句，修改库存数量，根据id修改
        param.setId(result.getId());
        ryInventoryCurrentStockMapper.updateRyInventoryCurrentStock(param);

        //出库记录新增
        OutRyDepotItem outRyDepotItem = new OutRyDepotItem();
        outRyDepotItem.setInventoryId(ryDepotItemVo.getInventoryId());
        outRyDepotItem.setDepotId(ryDepotItemVo.getDepotId());
        outRyDepotItem.setAllPrice(ryDepotItemVo.getAllPrice());
        outRyDepotItem.setOperNumber(ryDepotItemVo.getOperNumber());//出库数量
        outRyDepotItem.setOperTime(DateUtils.getNowDate());
        outRyDepotItem.setRemark(ryDepotItemVo.getRemark());
        outRyDepotItem.setHeaderType("1");//1代表出库
        outRyDepotItem.setTenantId(ryDepotItemVo.getHeaderId());//出库所关联的项目
        outRyDepotItem.setUpdateTime(new Date());

        return outRyDepotItemMapper.insertOutRyDepotItem(outRyDepotItem);
    }

    /**
     * 修改出库
     *
     * @param outRyDepotItem 出库
     * @return 结果
     */
    @Override
    public int updateOutRyDepotItem(OutRyDepotItem outRyDepotItem)
    {
        return outRyDepotItemMapper.updateOutRyDepotItem(outRyDepotItem);
    }

    /**
     * 批量删除出库
     *
     * @param ids 需要删除的出库主键
     * @return 结果
     */
    @Override
    public int deleteOutRyDepotItemByIds(Long[] ids)
    {
        return outRyDepotItemMapper.deleteOutRyDepotItemByIds(ids);
    }

    /**
     * 删除借出信息
     *
     * @param id 出库主键
     * @return 结果
     */
    @Override
    public int deleteOutRyDepotItemById(Long id)
    {
        return outRyDepotItemMapper.deleteOutRyDepotItemById(id);
    }
}
